/**
 * @Author: bzirs
 * @Date: 2021-05-10 09:38:15
 * @LastEditors: bzirs
 * @LastEditTime: 2022-12-25 20:38:00
 * @FilePath: /vue2-hr-two/src/store/modules/user.js
 * @Description:
 * @
 * @Copyright (c) 2022 by bzirs, All Rights Reserved.
 */
/**
 * @Author: bzirs
 * @Date: 2021-05-10 09:38:15
 * @LastEditors: bzirs
 * @LastEditTime: 2022-12-24 20:06:25
 * @FilePath: /vue2-hr-two/src/store/modules/user.js
 * @Description:
 * @
 * @Copyright (c) 2022 by bzirs, All Rights Reserved.
 */
import { getToken, removeToken, setToken } from '@/utils/auth'
import { getAdminInfo, getUserOtherInfo, login as toLogin } from '@/api/user'

const getUserState = _ => ({
  token: getToken(),
  userInfo: {}
})

const state = getUserState()

const mutations = {
  // 更新token
  updateToken(state, payload) {
    state.token = payload
    setToken(payload)
  },
  // 更新用户信息
  updateUserInfo(state, payload) {
    console.log(payload)
    state.userInfo = payload
  },
  // 退出登录
  toLogout(state, payload) {
    removeToken()
    state.token = null
    state.userInfo = {}
  }
}

const actions = {
  async login({ commit }, payload) {
    // 发送登录请求
    const { data } = await toLogin(payload)
    commit('updateToken', data)
  },
  // 获取用户相关信息
  async getUserInfo({ commit }, payload) {
    const { data } = await getAdminInfo()
    const { data: otherData } = await getUserOtherInfo(data.userId)

    console.log(data, otherData)
    commit('updateUserInfo', { ...data, ...otherData })
  }

}

export default {
  namespaced: true,
  state,
  mutations,
  actions
}
